package meijuli.com.join51.business.network;

import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkRequest;
import android.os.Build;

import meijuli.com.join51.business.utils.PLog;

import org.jetbrains.anko.AsyncKt;

import java.util.ArrayList;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import me.foji.anko.ContextKt;

/**
 * 网络状态监视器
 *
 * @author Wizong 2017-05-19 13:09
 */
public class NetworkMonitor {
    private static Context mApplicationContext;
    private static ArrayList<OnNetworkListener> mListeners = new ArrayList<>();
    private static NetworkReceiver mNetworkReceiver = new NetworkReceiver(mListeners);

    public static void init(Context applicationContext) {
        mApplicationContext = applicationContext;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            ConnectivityManager manager = ContextKt.connectivityManager(mApplicationContext);
            manager.registerNetworkCallback(new NetworkRequest.Builder().build(), new ConnectivityManager.NetworkCallback() {
                @Override
                public void onAvailable(Network network) {
                    super.onAvailable(network);

                    AsyncKt.runOnUiThread(mApplicationContext, new Function1<Context, Unit>() {
                        @Override
                        public Unit invoke(Context context) {
                            for (OnNetworkListener mListener : mListeners) {
                                mListener.onNetworkConnected(NetworkUtil.networkType());
                            }
                            return Unit.INSTANCE;
                        }
                    });
                }

                @Override
                public void onLosing(Network network, int maxMsToLive) {
                    super.onLosing(network, maxMsToLive);
                    PLog.d("网络将要断开");
                }

                @Override
                public void onLost(Network network) {
                    super.onLost(network);
                    PLog.d("网络断开");

                    AsyncKt.runOnUiThread(mApplicationContext, new Function1<Context, Unit>() {
                        @Override
                        public Unit invoke(Context context) {

                            for (OnNetworkListener mListener : mListeners) {
                                mListener.onNetworkDisconnected();
                            }
                            return Unit.INSTANCE;
                        }
                    });
                }
            });
        } else {
            IntentFilter filter = new IntentFilter();
            filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
            filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");

            mApplicationContext.registerReceiver(mNetworkReceiver, filter);
        }
    }

    public static void registerListener(OnNetworkListener listener) {
        mListeners.add(listener);
    }

    public static void removeListener(OnNetworkListener listener) {
        mListeners.remove(listener);
    }

    public static void onDestory() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            mApplicationContext.unregisterReceiver(mNetworkReceiver);
        }
        mListeners.clear();
    }

    enum NetworkType {
        WIFI("WIFI"),
        _2G("_2G"),
        _3G("_3G"),
        _4G("_4G"),
        UNKNOWN("UNKNOWN");

        private String mNetwork;

        NetworkType(String network) {
            mNetwork = network;
        }

        @Override
        public String toString() {
            return mNetwork;
        }
    }

    interface OnNetworkListener {

        /**
         * 网络已连接
         *
         * @param type 网络连接的类型
         */
        void onNetworkConnected(NetworkType type);

        /**
         * 网络断开
         */
        void onNetworkDisconnected();
    }
}

